<?php

/**
 * temporaryoutput actions.
 *
 * @package    student_manager
 * @subpackage temporaryoutput
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class temporaryoutputActions extends autotemporaryoutputActions
{
  

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['tempno_is_empty']))
    {
      $criterion = $c->getNewCriterion(TemporaryOutputPeer::TEMPNO, '');
      $criterion->addOr($c->getNewCriterion(TemporaryOutputPeer::TEMPNO, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['tempno']) && $this->filters['tempno'] !== '')
    {
      $c->add(TemporaryOutputPeer::TEMPNO, '%'.strtr($this->filters['tempno'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['type_is_empty']))
    {
      $criterion = $c->getNewCriterion(TemporaryOutputPeer::TYPE, '');
      $criterion->addOr($c->getNewCriterion(TemporaryOutputPeer::TYPE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['type']) && $this->filters['type'] !== '')
    {
      $c->add(TemporaryOutputPeer::TYPE, $this->filters['type']);
    }
    if (isset($this->filters['created_at_is_empty']))
    {
      $criterion = $c->getNewCriterion(TemporaryOutputPeer::CREATED_AT, '');
      $criterion->addOr($c->getNewCriterion(TemporaryOutputPeer::CREATED_AT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_at']))
    {
      if (isset($this->filters['created_at']['from']) && $this->filters['created_at']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(TemporaryOutputPeer::CREATED_AT, $this->filters['created_at']['from'], Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_at']['to']) && $this->filters['created_at']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(TemporaryOutputPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(TemporaryOutputPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
  }
  public function executeEdit()
  {
    $this->temporary_output = $this->getTemporaryOutputOrCreate();

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updateTemporaryOutputFromRequest();
      $this->temporary_output->setTempno(CommonValues::GenerateAutoNumber('TpOUT', $this->temporary_output->getTempno(), 'TpO'));
      //Tien code here
              $qty1s = $this->getRequestParameter('qty1s');
              $qty2s = $this->getRequestParameter('qty2s');
              $rowIds = $this->getRequestParameter('rowIds');
              
              if (isset($rowIds) && count($rowIds) && isset($qty1s) && count($qty1s) ) {
              	if ($this->temporary_output->getComponentgroupId()) {
              	    $c = new Criteria();
              	    $c->add(ComponentsPeer::COMPONENTGROUP_ID , $this->temporary_output->getComponentgroupId());
              	    ComponentsPeer::doDelete($c);
              	}
              	$componentGroup = new ComponentGroup();
              	if ($componentGroup)    {
              		$componentGroup->setType(1);
              		$componentGroup->save();
              		$componentGroupId = $componentGroup->getId();
              		
              		if ($componentGroupId) {
              			$this->temporary_output->setComponentgroupId($componentGroupId);
              			for($i=0; $i < count($rowIds); $i++){
              				$components = new Components();
              				if ($components){
              					$components->setComponentgroupId($componentGroupId);
              					$components->setComponentId($rowIds[$i]);
              					$components->setQty1($qty1s[$i]);
              					$components->setQty2($qty2s[$i]);
              					$components->save();
              				}
              			}	      			
              		}
              	}
              }

      try
      {
        $this->saveTemporaryOutput($this->temporary_output);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Temporary outputs.');
        return $this->forward('temporaryoutput', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('temporaryoutput/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('temporaryoutput/list');
      }
      else
      {
        return $this->redirect('temporaryoutput/edit?id='.$this->temporary_output->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }
  
  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/temporary_output/filters');

    // pager
    $this->pager = new sfPropelPager('TemporaryOutput', 25);
    $c = new Criteria();
    $c->addDescendingOrderByColumn(TemporaryOutputPeer::ID);
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/temporary_output')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/temporary_output');
    }
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->temporary_output = $this->getTemporaryOutputOrCreate();
    $this->updateTemporaryOutputFromRequest();

    $this->labels = $this->getLabels();
    
    //transfer data to edit_form template
    $this->temporary_output->qty1s = $this->getRequestParameter('qty1s');
    $this->temporary_output->qty2s = $this->getRequestParameter('qty2s');
    $this->temporary_output->rowIds = $this->getRequestParameter('rowIds');

    return sfView::SUCCESS;
  }
protected function getLabels()
  {
    return array(
      'temporary_output{id}' => 'Id:',
      'temporary_output{tempno}' => 'TemporaryOutput Number:',
      'temporary_output{type}' => 'Type:',
      'temporary_output{notes}' => 'Notes:',
      'temporary_output{componentgroup_id}' => 'Component List:',
      'temporary_output{created_at}' => 'Created at:',
      'qty1s' => 'Component List:',
      'qty2s' => 'Component List:',
    );
  }
}
